package com.loong.web.gp4j.service.code;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.loong.web.gp4j.bean.schema.code.CodeSchema;
import com.loong.web.gp4j.bean.vo.code.CVo;

/**
 * 代码封装器
 *
 * @author 张成轩
 */
public class CodeAdapter {

	/** 代码排序器 */
	public static Comparator<CodeSchema> codeComparator = new Comparator<CodeSchema>() {

		@Override
		public int compare(CodeSchema o1, CodeSchema o2) {

			return o1.getOrder().compareTo(o2.getOrder());
		}
	};

	/**
	 * @param schemas
	 * @return 代码映射
	 */
	public static Map<Integer, String> build(List<CodeSchema> schemas) {

		Map<Integer, String> codes = new LinkedHashMap<Integer, String>();
		Collections.sort(schemas, codeComparator);
		for (CodeSchema schema : schemas)
			codes.put(schema.getValue(), schema.getName());
		return codes;
	}

	/**
	 * @param schemas
	 * @param vos
	 */
	public static void convert(List<CodeSchema> schemas, List<CVo> vos) {

		Collections.sort(schemas, codeComparator);
		for (CodeSchema schema : schemas) {

			CVo vo = new CVo();
			vos.add(vo);
			convert(schema, vo);
		}
	}

	/**
	 * @param schema
	 * @param vo
	 */
	public static void convert(CodeSchema schema, CVo vo) {

		vo.setId(schema.getId());
		vo.setName(schema.getName());
		vo.setValue(schema.getValue());
		vo.setOrder(schema.getOrder());
	}
}
